package com.audaque.springboot.foshanupload.core.util.ssl;



import com.audaque.springboot.foshanupload.core.util.ssl.model.AnyTrustManager;
import com.audaque.springboot.foshanupload.core.util.ssl.model.KeyStoreParam;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.function.Predicate;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

public class SslUtils {
    public SslUtils() {
    }

    public static SSLContext createSSLContext(String protocol, boolean trustAll, KeyStoreParam keyStoreParam, String keyManagerType) throws Exception {
        if (StringUtils.isBlank(protocol)) {
            return null;
        } else {
            SSLContext sslContext = SSLContext.getInstance(protocol);
            TrustManager[] tm;
            if (trustAll) {
                tm = new TrustManager[]{new AnyTrustManager((Predicate)null)};
            } else if (keyStoreParam != null) {
                tm = createTrustManager(keyStoreParam, keyManagerType);
            } else {
                tm = null;
            }

            sslContext.init((KeyManager[])null, tm, (SecureRandom)null);
            return sslContext;
        }
    }

    public static SSLSocketFactory createSSLSocketFactory(String protocol, boolean trustAll, KeyStoreParam keyStoreParam, String keyManagerType) throws Exception {
        if (StringUtils.isBlank(protocol)) {
            return null;
        } else {
            SSLContext sslContext = createSSLContext(protocol, trustAll, keyStoreParam, keyManagerType);
            return sslContext == null ? null : sslContext.getSocketFactory();
        }
    }

    public static TrustManager[] createTrustManager(KeyStoreParam keyStoreParam, String keyManagerType) throws Exception {
        if (keyStoreParam == null) {
            return null;
        } else if (keyStoreParam.getType() != null && (keyStoreParam.getFilePath() != null || keyStoreParam.getKeyBytes() != null) && keyStoreParam.getPwd() != null && keyManagerType != null) {
            KeyStore keyStore = KeyStore.getInstance(keyStoreParam.getType());
            TrustManagerFactory tmf;
            TrustManager[] var5;
            if (keyStoreParam.getKeyBytes() != null) {
                ByteArrayInputStream is = new ByteArrayInputStream(keyStoreParam.getKeyBytes());

                try {
                    keyStore.load(is, keyStoreParam.getPwd().toCharArray());
                    tmf = TrustManagerFactory.getInstance(keyManagerType);
                    tmf.init(keyStore);
                    var5 = tmf.getTrustManagers();
                } catch (Throwable var8) {
                    try {
                        is.close();
                    } catch (Throwable var7) {
                        var8.addSuppressed(var7);
                    }

                    throw var8;
                }

                is.close();
                return var5;
            } else {
                FileInputStream is = new FileInputStream(keyStoreParam.getFilePath());

                try {
                    keyStore.load(is, keyStoreParam.getPwd().toCharArray());
                    tmf = TrustManagerFactory.getInstance(keyManagerType);
                    tmf.init(keyStore);
                    var5 = tmf.getTrustManagers();
                } catch (Throwable var9) {
                    try {
                        is.close();
                    } catch (Throwable var6) {
                        var9.addSuppressed(var6);
                    }

                    throw var9;
                }

                is.close();
                return var5;
            }
        } else {
            throw new RuntimeException("参数错误");
        }
    }
}
